﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DLL;

namespace BLL
{
    public class PhieuGiamGiaBLL
    {
        public String tensploai1_theoId_dd(int id)
        {
            DataTable phieu = LinqToDataTable(DB.phieugiamgias.Where(q => q.Id_phieu == id));
            string Id = phieu.Rows[0][10].ToString();

            DataTable sploai1 = LinqToDataTable(DB.sanpham_loai1s.Where(q => q.Id_sploai1 == Id));
            String ten = sploai1.Rows[0][1].ToString();

            return ten;
        }
        luuliDataContext DB = new luuliDataContext();
        //lay phieu giam gia nhieu nhat
        public DataTable LayGiamGiaNhieuNhat()
        {
            DataTable a = LinqToDataTable(DB.phieugiamgias.OrderByDescending(q => q.Phantramgiam_phieu).Take(3));
            return a;

        }
        //lay phieu giam gia moi nhat
        public DataTable layPhieunew()
        {
            DataTable a = LinqToDataTable(DB.phieugiamgias.Where(q => q.Ngayketthuc_phieu > DateTime.Now).Take(3).OrderByDescending(q => q.Ngaytao_phieu));
            return a;
        }
        //lay phan tram giam gia phieu
        public DataTable LayPGiamGia(int phantramgiamgia)
        {
            DataTable pgiamgia = LinqToDataTable(DB.phieugiamgias.Where(p => (p.Phantramgiam_phieu >= phantramgiamgia)).Take(4).OrderByDescending(q => q.Phantramgiam_phieu));
            return pgiamgia;
        }
        //lay tat ca phieu giam gia
        public DataTable LayPGiamGia(string TP)
        {
            //DataTable a = LinqToDataTable(DB.thanhphos.Where(q=>q.Id_tp == tp));
            //string id = a.Rows[0][0].ToString();
            DataTable b = LinqToDataTable(DB.phieugiamgias.Where(q => q.Id_tp_PGG == TP));
            return b;
        }
        public DataTable LinqToDataTable<T>(System.Collections.Generic.IEnumerable<T> varlist)
        {
            try
            {
                System.Data.DataTable dtReturn = new System.Data.DataTable();
                System.Reflection.PropertyInfo[] oProps = null;
                if (varlist == null) return dtReturn;
                foreach (T rec in varlist)
                {
                    if (oProps == null)
                    {
                        oProps = ((Type)rec.GetType()).GetProperties();
                        foreach (System.Reflection.PropertyInfo pi in oProps)
                        {
                            Type colType = pi.PropertyType;
                            if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                            {
                                colType = colType.GetGenericArguments()[0];
                            }
                            dtReturn.Columns.Add(new System.Data.DataColumn(pi.Name, colType));
                        }
                    }
                    System.Data.DataRow dr = dtReturn.NewRow();
                    foreach (System.Reflection.PropertyInfo pi in oProps)
                    {
                        dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
                    }
                    dtReturn.Rows.Add(dr);
                }
                return dtReturn;
            }
            catch (Exception ex) { return null; }

        }
        //lay 1 phieu giam gia
        public DataTable selectdetail(int id)
        {
            DataTable a = LinqToDataTable(DB.phieugiamgias.Where(q => q.Id_phieu == id));
            return a;
        }
    }
}
